package ua.at.tsvetkov.samizdat.services;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.data.AsyncTaskTextDownload;
import ua.at.tsvetkov.util.Log;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class SrvTextDownload extends Service {

	private IBinder	binder;

	public class LocalBinder extends Binder {
		SrvTextDownload getService() {
			return SrvTextDownload.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.v(getString(R.string.SRV_TEXT_DOWNLOAD_BINDED));
		return binder;
	}

	@Override
	public void onCreate() {
		binder = new LocalBinder();
	}

	@Override
	public void onStart(Intent intent, int startid) {
		Log.v(getString(R.string.SRV_TEXT_DOWNLOAD_STARTED));
		Intent broadcastIntent = new Intent(Const.BROADCAST_MESSAGE).putExtra(Const.SERVICE_TEXT_DOWNLOAD_STARTED, Const.SERVICE_TEXT_DOWNLOAD_STARTED);
		sendBroadcast(broadcastIntent);
		AsyncTaskTextDownload asyncTaskTextDownload = new AsyncTaskTextDownload(this, intent.getStringExtra("BOOK_URL"), intent.getIntExtra("BOOK_ID", -1));
		asyncTaskTextDownload.execute();
	}

	@Override
	public void onDestroy() {
		Log.v(getString(R.string.SRV_TEXT_DOWNLOAD_STOPED));
		Intent broadcastIntent = new Intent(Const.BROADCAST_MESSAGE).putExtra(Const.SERVICE_TEXT_DOWNLOAD_STOPED, Const.SERVICE_TEXT_DOWNLOAD_STOPED);
		sendBroadcast(broadcastIntent);
	}
}
